package com.ruyuan.user.server.domain.converter;

import com.ruyuan.common.model.Features;
import com.ruyuan.user.api.model.enums.AuthResourceTypeEnum;
import com.ruyuan.user.server.domain.model.AuthResource;
import com.ruyuan.user.server.domain.model.Authority;
import com.ruyuan.user.server.domain.service.AuthorityDomainService;
import com.ruyuan.user.server.repository.bean.AuthResourceDO;
import com.ruyuan.user.server.repository.bean.AuthorityDO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * @author xx
 */
@Component
public class AuthorityConverter {

    @Autowired
    private AuthorityDomainService authorityDomainService;

    public AuthResourceDO convertToAuthResourceDO(AuthResource authResource) {
        if (authResource == null) {
            return null;
        }
        AuthResourceDO authResourceDO = new AuthResourceDO();
        BeanUtils.copyProperties(authResource, authResourceDO);
        if (!authResource.getFeatures().isEmpty()) {
            authResourceDO.setFeatures(authResource.getFeatures().toString());
        }
        authResourceDO.setType(authResource.getType().getCode());
        authResourceDO.setIsDeleted("n");
        if (authResource.isDeleted()) {
            authResourceDO.setIsDeleted("y");
        }
        authResourceDO.setCreator(authResource.getModifier());
        authResourceDO.setGmtCreate(new Date());
        authResourceDO.setGmtModified(new Date());
        return authResourceDO;
    }

    public Authority convertAuthorityDOToModel(AuthorityDO authorityDO) {
        if (authorityDO == null) {
            return null;
        }
        Authority authority = new Authority();
        BeanUtils.copyProperties(authorityDO, authority);
        if (StringUtils.isNotBlank(authorityDO.getFeatures())) {
            authority.setFeatures(Features.of(authorityDO.getFeatures()));
        }
        authority.setDeleted("y".equals(authorityDO.getIsDeleted()));
        return authority;
    }

    public AuthorityDO convertAuthorityToDO(Authority authority) {
        if (authority == null) {
            return null;
        }
        AuthorityDO authorityDO = new AuthorityDO();
        BeanUtils.copyProperties(authority, authorityDO);
        if (!authority.getFeatures().isEmpty()) {
            authorityDO.setFeatures(authority.getFeatures().toString());
        }
        authorityDO.setIsDeleted("n");
        if (authority.isDeleted()) {
            authorityDO.setIsDeleted("y");
        }
        authorityDO.setCreator(authority.getModifier());
        authorityDO.setGmtCreate(new Date());
        authorityDO.setGmtModified(new Date());
        return authorityDO;
    }

    public AuthResource convertAuthResourceDOToModel(AuthResourceDO authResourceDO) {
        if (authResourceDO == null) {
            return null;
        }
        AuthResource authResource = new AuthResource();
        BeanUtils.copyProperties(authResourceDO, authResource);
        if (org.apache.commons.lang.StringUtils.isNotEmpty(authResourceDO.getFeatures())) {
            authResource.setFeatures(Features.of(authResourceDO.getFeatures()));
        } else {
            authResource.setFeatures(Features.of());
        }
        authResource.setType(AuthResourceTypeEnum.getByCode(authResourceDO.getType()));
        authResource.setDeleted("y".equals(authResourceDO.getIsDeleted()));
        authResource.setRelatedAuthorities(authorityDomainService.queryAuthorityByResource(authResource.getId()));
        return authResource;
    }

}
